<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceToDoList extends core {

    public function process() {
//        $limit = $this->limit;
//        $page = intval($_POST['page']);       
//        if ($page < 1) {
//            $page = 1;
//        }
//        $from = ($page - 1) * $limit;
        $user_id = intval($_POST['user_id']);
        //past
        
        $total_past = $this->db->get_row("SELECT count(todo_id) as total FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date)< CURDATE() ;");
        $html = '<li class="wd-abc act_up_down">
                                <span class="wd-down up " rel="past"><img src="img/up.png"/></span>
                                <span class="wd-txt-abc">Past</span>
                            </li>';
        if ($total_past->total > 0) {
            $data_past = $this->db->get_results("SELECT * FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date) < CURDATE() ORDER BY start_time asc LIMIT 0,2;");
            foreach ($data_past as $todo) {
                $todo_start_time = strtotime($todo->start_time);
                $start_date = date('D m/d/Y', strtotime($todo->start_date));
                if ($todo->end_time == $todo->start_date) {
                    $time = date('h:i A', $todo_start_time);
                } else {
                    $time = date('h:i A', $todo_start_time) . ' - ' . date('h:i A', strtotime($todo->end_time));
                }
                
                $checked='';
                if($todo->is_done==1){
                    $checked='checked=checked';
                }
                $html.='<li class="wd-list-item-guest wd-list-item-todo past">
                                <div class="wd-list-todo-check">
                                    <input type="checkbox" '.$checked.'  rel="'.$todo->todo_id.'" id="wd-checkbox-'.$todo->todo_id.'" name="" class="regular-checkbox-addguest wd-checkbox">
                                    <label for="wd-checkbox-'.$todo->todo_id.'" class="label-checkbox"></label>
                                </div>
                                <div class="wd-list-guest-action">
                                    
                                    <span style="float: right; margin-right: 4px;" class="wd-delete-guest" rel="'.$todo->todo_id.'"><img src="img/delete-icon.png" /></span>
                                </div>
                                <div class="wd-list-guest-content">
                                    <div class="wd-name-guest">' . $todo->todo_item . '</div>
                                    <div class="wd-contact">' . $start_date . '   |   ' . $time . '</div>
                                    
                                    
                                </div>
                            </li>';
            }
        }
        if ($total_past->total > 2) {
            $html.='<li class="wd-list-item-guest showmore past" rel="past">
                        <input type="hidden" name="page" id="page_past" value="1"/>
                        <input type="hidden" name="end_paging" id="end_paging_past" value="0"/>
                        <div class="wd-name-guest ">Show more</div>
                    </li>';
        }
        //today
        
        $total_today = $this->db->get_row("SELECT count(todo_id) as total FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date)=CURDATE() ;");

//        if (ceil($total->total / $limit) <= $page) {
//            $end_paging = 1;
//        } else {
//            $end_paging = 0;
//        }
        $html .= '<li class="wd-abc act_up_down">
                                <span class="wd-down up " rel="today"><img src="img/up.png"/></span>
                                <span class="wd-txt-abc">To day</span>
                            </li>';
        if ($total_today->total > 0) {
            $data_today = $this->db->get_results("SELECT * FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date)=CURDATE() ORDER BY start_time asc LIMIT 0,2;");
            foreach ($data_today as $todo) {
                  $todo_start_time = strtotime($todo->start_time);
                $start_date = date('D m/d/Y', strtotime($todo->start_date));
                if ($todo->end_time == $todo->start_date) {
                    $time = date('h:i A', $todo_start_time);
                } else {
                    $time = date('h:i A', $todo_start_time) . ' - ' . date('h:i A', strtotime($todo->end_time));
                }
                
                $checked='';
                if($todo->is_done==1){
                    $checked='checked=checked';
                }
                $html.='<li class="wd-list-item-guest wd-list-item-todo today">
                                <div class="wd-list-todo-check">
                                    <input type="checkbox" '.$checked.'  rel="'.$todo->todo_id.'" id="wd-checkbox-'.$todo->todo_id.'" name="" class="regular-checkbox-addguest wd-checkbox">
                                    <label for="wd-checkbox-'.$todo->todo_id.'"></label>
                                </div>
                                <div class="wd-list-guest-action">
                                    <a href="editToDo.html" class="wd-edit-guest" rel="'.$todo->todo_id.'"><img src="img/edit-icon.png" /></a>
                                    <span class="wd-delete-guest" rel="'.$todo->todo_id.'"><img src="img/delete-icon.png" /></span>
                                </div>
                                <div class="wd-list-guest-content">
                                    <div class="wd-name-guest">' . $todo->todo_item . '</div>
                                    <div class="wd-contact">' . $start_date . '   |   ' . $time . '</div>
                                    
                                    
                                </div>
                            </li>';
            }
        }
        if ($total_today->total > 2) {
            $html.='<li class="wd-list-item-guest showmore today" rel="today">
                        <input type="hidden" name="page" id="page_today" value="1"/>
                        <input type="hidden" name="end_paging" id="end_paging_today" value="0"/>
                        <div class="wd-name-guest ">Show more</div>
                    </li>';
        }
        //tomorrow
        
        $total_tomorrow = $this->db->get_row("SELECT count(todo_id) as total FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date) >= DATE_ADD(CURDATE(),INTERVAL 1 DAY) AND DATE(start_date) < DATE_ADD(CURDATE(),INTERVAL 2 DAY) ;");
        $html .= '<li class="wd-abc act_up_down">
                                <span class="wd-down up " rel="tomorrow"><img src="img/up.png"/></span>
                                <span class="wd-txt-abc">Tomorrow</span>
                            </li>';
        if ($total_tomorrow->total > 0) {
            $data_tomorrow = $this->db->get_results("SELECT * FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date) >= DATE_ADD(CURDATE(),INTERVAL 1 DAY) AND DATE(start_date) < DATE_ADD(CURDATE(),INTERVAL 2 DAY) ORDER BY start_time asc LIMIT 0,2;");
            foreach ($data_tomorrow as $todo) {
                    
                 $todo_start_time = strtotime($todo->start_time);
                $start_date = date('D m/d/Y', strtotime($todo->start_date));
                if ($todo->end_time == $todo->start_date) {
                    $time = date('h:i A', $todo_start_time);
                } else {
                    $time = date('h:i A', $todo_start_time) . ' - ' . date('h:i A', strtotime($todo->end_time));
                }
                
                $checked='';
                if($todo->is_done==1){
                    $checked='checked=checked';
                }
                $html.='<li class="wd-list-item-guest wd-list-item-todo tomorrow">
                                <div class="wd-list-todo-check">
                                    <input type="checkbox" '.$checked.' rel="'.$todo->todo_id.'" id="wd-checkbox-'.$todo->todo_id.'" name="" class="regular-checkbox-addguest wd-checkbox">
                                    <label for="wd-checkbox-'.$todo->todo_id.'"></label>
                                </div>
                                <div class="wd-list-guest-action">
                                    <a href="editToDo.html" class="wd-edit-guest" rel="'.$todo->todo_id.'"><img src="img/edit-icon.png" /></a>
                                    <span class="wd-delete-guest" rel="'.$todo->todo_id.'"><img src="img/delete-icon.png" /></span>
                                </div>
                                <div class="wd-list-guest-content">
                                    <div class="wd-name-guest">' . $todo->todo_item . '</div>
                                    <div class="wd-contact">' . $start_date . '   |   ' . $time . '</div>
                                    
                                    
                                </div>
                            </li>';
            }
        }
        if ($total_tomorrow->total > 2) {
            $html.='<li class="wd-list-item-guest showmore tomorrow" rel="tomorrow">
                        <input type="hidden" name="page" id="page_tomorrow" value="1"/>
                        <input type="hidden" name="end_paging" id="end_paging_tomorrow" value="0"/>
                        <div class="wd-name-guest ">Show more</div>
                    </li>';
        }
        //later
        $html .= '<li class="wd-abc act_up_down">
                                <span class="wd-down up" rel="later"><img src="img/up.png"/></span>
                                <span class="wd-txt-abc">Later</span>
                            </li>';
        $total_later = $this->db->get_row("SELECT count(todo_id) as total FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date) >= DATE_ADD(CURDATE(),INTERVAL 2 DAY) ;");
        if ($total_later->total > 0) {
            $data_later = $this->db->get_results("SELECT * FROM todo_lists WHERE user_id={$user_id} AND DATE(start_date) >= DATE_ADD(CURDATE(),INTERVAL 2 DAY) ORDER BY start_time asc LIMIT 0,2;");
            foreach ($data_later as $todo) {
                     
                  $todo_start_time = strtotime($todo->start_time);
                $start_date = date('D m/d/Y', strtotime($todo->start_date));
                if ($todo->end_time == $todo->start_date) {
                    $time = date('h:i A', $todo_start_time);
                } else {
                    $time = date('h:i A', $todo_start_time) . ' - ' . date('h:i A', strtotime($todo->end_time));
                }
                
                $checked='';
                if($todo->is_done==1){
                    $checked='checked=checked';
                }
                $html.='<li class="wd-list-item-guest wd-list-item-todo later">
                                <div class="wd-list-todo-check">
                                    <input type="checkbox" '.$checked.' rel="'.$todo->todo_id.'" id="wd-checkbox-'.$todo->todo_id.'" name="" class="regular-checkbox-addguest wd-checkbox">
                                    <label for="wd-checkbox-'.$todo->todo_id.'"></label>
                                </div>
                                <div class="wd-list-guest-action">
                                    <a href="editToDo.html" class="wd-edit-guest" rel="'.$todo->todo_id.'"><img src="img/edit-icon.png" /></a>
                                    <span class="wd-delete-guest" rel="'.$todo->todo_id.'"><img src="img/delete-icon.png" /></span>
                                </div>
                                <div class="wd-list-guest-content">
                                    <div class="wd-name-guest">' . $todo->todo_item . '</div>
                                    <div class="wd-contact">' . $start_date . '   |   ' . $time . '</div>
                                    
                                    
                                </div>
                            </li>';
            }
        }
        if ($total_later->total > 2) {
            $html.='<li class="wd-list-item-guest showmore later" rel="later">
                        <input type="hidden" name="page" id="page_later" value="1"/>
                        <input type="hidden" name="end_paging" id="end_paging_later" value="0"/>
                        <div class="wd-name-guest ">Show more</div>
                    </li>';
        }
        if ($html == '') {
            $html = 'No task was found.';
        }
        $this->_JSONResponse(array('error' => 0, 'html' => $html));
    }

}

$processor = new webServiceToDoList();
$processor->process();
?>